COSC 1550 – Introduction to Programming
Test Four
Name____________________
Multiple Choice
Circle the
letter of the statement that bests
answers/completes each question. Each
question is worth three points.
1. Which one of the following statements correctly creates an
array containing exactly 5 elements, each element containing an associated
value, not a default value.
a) float Ratings [ ] = {1.0, 2.0, 3.0, 4.0, 5.0};
b) float Ratings [ 7 ] = {1.0, 2.0, 3.0, 4.0, 5.0};
c) float Ratings [ 5 ] = {1.0, 2.0, 3.0};
d) float Ratings [ ] =
5*{0.0};
2. Which one of the following array
declarations is correct?
a) salary
float [50];
b) float salary [50];
c) salary float array [50];
d) array float salary [50];
3. Assume there is an array called degree
that contains the following values (in this sequence): {15, 17, 19, 21} The
statement
cout << degree [3] << endl;
will display
a) 15
b) 17
c) 19
d) 21
4. Which one of the following statements
regarding arrays is not correct?
a) An
array can contain multiple values.
b) Each
element in an array can be referenced by the name of the array followed by a subscript
enclosed in square brackets [ ].
c) C++
does no bounds checking on arrays.
d) If
an array's name is referenced without any subscript, this means the sum of all of the array
elements.
5. Given the following declarations:
int Number [4] = { 2, 8, 16, 25 };
int sub = 0;
The statement
cout << Number [sub+2] << endl;
will display
a) 2
b) 8
c) 16
d) 25
6. What is the result of the following
code assuming the declarations in the previous problem?
for (
sub = 1; sub < 4; sub++)
{ Number [sub] = 0; }
a) Each element of the array is
set equal to 0.
b) Each element of the array,
except the first one, is set equal to 0.
c) Each element of the array,
except the last one, is set equal to 0.
d) Unpredictable results occur
since the array is referenced beyond its bounds.
7. What is the result of the following
code assuming the declarations in the above problem?
int
sum = 0;
for ( sub = 1; sub
< 4; sub = sub +2)
{
sum = sum + Number [sub]; }
a) The first and second elements
are added together and assigned to the
variable sum.
b) The first and third elements
are added together and assigned to the
variable sum.
c) The second and third elements
are added together and assigned to the
variable sum.
d) The
second and fourth elements are added together and assigned to the variable sum.
8 An array can store a group of values, but the values must
be
a. the same data type
b. each of a different data type
c. constants
d. integers
9. An array's size declarator must be a(n)______ with a value greater than ______.
a. number, one
b. number, zero
c. constant integer expression, zero
d. variable, -1
10. Subscript numbering in C++
a. can be set at runtime
b. can begin with a programmer-defined value
c. varies from program to program
d. automatically begins with zero
11. Arrays may be ___________ at the time
they are __________.
a. resized, executed
b. re-scoped, deleted
c. initialized, declared
d. pre-compiled, typecast
12. Given the following declaration, where is 77 stored in the array called scores?
int
scores[3][4]={{83, 62, 77, 97},
{90, 78, 81,92}
{91, 80, 88, 94}}:
a.
scores[3][4]
b.
scores[1][3]
c.
scores[0][2]
d.
scores[2][0]
13. An array can easily be stepped through by
using a
a. reference variable
b. for loop
c. named constant
d. null value
14. To assign the contents of one array to another,
you must use the
a. assignment operator
b. equality operator
c. array names
d. a loop that contains an instruction that sets each element
of an array to the associated element of the array being copied.
15. To pass an array as an argument to a function,
pass the _________ of the array.
a. contents
b. size, expressed as an integer
c. name
d. value of the first element
16. A two-dimensional array can have data
elements of ______ data type(s).
a. one
b. two
c. four
d. Any of
these
17. A two-dimensional array of characters can
contain
a. strings of the same length
b. strings of different lengths
c. unused elements
d. All of
these
18. A(n)
____________ can indicate starting values of an array.
a. initialization list
b. array name
c. subscript
d. element
19. The __________ is automatically appended
to a character array when it is initialized with a string constant.
a. array name
b. null terminator
c. value of the first element
d. number of elements
20. An array that will hold 5 names with 20
characters in the name would be declared using the _____________ statement.
a. char names[5][20];
b. char names[20][5];
c. char names[21][5];
d.
char names[5][21];
21. A(n) ________
search uses a loop to sequentially step through an array.
a. binary
b. unary
c. linear
d. relative
22. The _________ is adequate for searching
through small arrays.
a. binary search
b. linear search
c. unary search
d. bubble sort
23. ___________ algorithms are used to
arrange random data into some order.
a. Standard
search
b. Linear
c. Sorting
d. Binary
search
24. Which one of the following statements
regarding a binary search on an array is not
correct?
a) it is more efficient than a sequential search.
b) the values in the array must be in sequence.
c) if the search value is greater than the middle value, then
the first value is set
to the value at the (middle +1) element.
d) if the search value is less than the middle value, then the
middle value is set
to the value at the (last - 1) element.
25. Which one of the following sets of code
will correctly swap two adjacent elements in an array? You may assume all variables have been
properly declared.
a) hold = Array [index];
Array [index] = Array [index - 1];
Array [index - 1] = hold;
b) Array
[index - 1] = hold;
Array [index] = Array [index - 1];
hold = Array [index
- 1];
c) Array
[index - 1] = Array [index];
hold = Array [index - 1];
Array [index] = hold;
d) hold = Array [index];
Array [index - 1] = Array [index];
Array [index] = hold;
26. Which one of the following statements
correctly describes sort sequences?
a) Ascending
is low to high and descending is high to low.
b) Descending
is low to high and ascending is high to low.
c) Low-order is
low to high and high-order is high to low.
d) High-order
is low to high and low-order is high to low.
Problems
Find the errors in
each of the following code segments.
There may be multiple errors.
Each question is worth three points.
27. int size;
float values
[size];
28. int collections
[-20];
29. int hours [3] =
30. void showValues
(int nums);
{
for
(int i=0; i < 10; i++)
cout << nums [i];
}
Indicate how the data would appear after each
adjustment is made by the indicated sort algorithm. Make sure that you only perform one pass for
each line, similar to how the associated algorithm would work. (Note that every line may not be used).
Each
problem is worth five points each.
31. Selection
sort
Array[0] |
Array[1] |
Array[2] |
Array[3] |
Array[4] |
10 |
1 |
8 |
12 |
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32. Bubble
sort
Array[0] |
Array[1] |
Array[2] |
Array[3] |
Array[4] |
10 |
1 |
8 |
12 |
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|